package com.business.service.system;

import java.io.File;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import net.sf.rose.boot.BootStart;
import net.sf.rose.util.RoseRuntimeException;

/**
 * 类说明：封装了Spring的邮件发送服务
 * @author fengjian
 * @since 2019/08/07
 */
@Component
public class MailService implements BootStart {

    private static String HOST = "smtp.263.net";
    private static Integer PORT = 25;
    private static String USERNAME = "wisonnt.renlian@wison.com";
    private static String PASSWORD = "kr1D6TRb";
    private static String EMAILFORM = "wisonnt.renlian@wison.com";

    /**
     * 获取Spring邮件发送器
     *
     */
    public static JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(HOST);
        sender.setPort(PORT);
        sender.setUsername(USERNAME);
        sender.setPassword(PASSWORD);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", "100");
        p.setProperty("mail.smtp.auth", "true");
        sender.setJavaMailProperties(p);
        return sender;
    }

    private JavaMailSenderImpl mailSender = null;

    private MimeMessage mimeMessage = null;

    private MimeMessageHelper messageHelper = null;

    /**
     * 获取邮件服务对象实例
     *
     */
    public static MailService getInstance() {
        MailService service = new MailService();
        service.build();
        return service;
    }

    /**
     * 创建邮件服务对象实例
     */
    private void build() {
        mailSender = createMailSender();
        mimeMessage = mailSender.createMimeMessage();
        try {
            // 设置utf-8或GBK编码，否则邮件会有乱码
            messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            messageHelper.setFrom(EMAILFORM);
        } catch (Exception e) {
            throw new RoseRuntimeException(e);
        }
    }

    /**
     * 设置发送人
     * @param from
     * @param personal
     */
    public void setFrom(String from, String personal) {
        try {
            messageHelper.setFrom(from, personal);
        } catch (Exception e) {
            throw new RoseRuntimeException(e);
        }
    }

    /**
     * 添加附件
     * @param file
     * @param fileName
     */
    public void addAttachment(File file, String fileName) {
        FileSystemResource resource = new FileSystemResource(file);
        try {
            messageHelper.addAttachment(fileName, resource);
        } catch (Exception e) {
            throw new RoseRuntimeException(e);
        }
    }

    /**
     * 发送邮件
     *
     * @param to      接受人
     * @param subject 主题
     * @param html    发送内容
     * @throws Exception 异常
     */
    public void sendHtmlMail(String to, String subject, String html) throws Exception {
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(html, true);
        mailSender.send(mimeMessage);
    }

    /** 是否需要启动 */
    @Attribute
    private boolean needStart = false;

    /** 邮件服务 */
    @Element(required = false)
    private String host = "smtp.263.net";

    /** 邮件端口 */
    @Element(required = false)
    private int port = 25;

    /** 用户名 */
    @Element(required = false)
    private String userName = "fengjian@wison.com";

    /** 密码 */
    @Element(required = false)
    private String password = "1co@center%,.";

    /**
     * 需要启动
     */
    @Override
    public boolean needStart() {
        return needStart;
    }

    /**
     * 启动执行的方法
     */
    @Override
    public void start() {
        HOST = this.host;
        PORT = this.port;
        USERNAME = this.userName;
        PASSWORD = this.password;
        EMAILFORM = this.userName;
    }

    public static void main(String[] args) throws Exception {
        MailService service = MailService.getInstance();
        service.addAttachment(new File("e:/98700006.txt"), "附件");
        service.sendHtmlMail("9110530@qq.com", "test", "hello");
    }

}
